
import { reqLogin } from '@/api/user'
import { getToken, setToken } from '@/utils/auth'
const state = {
  token: getToken() || null
}
const mutations = {
  setToken(state, token) {
    state.token = token
  }
}
const actions = {
  login(context, data) {
    return new Promise((resolve, reject) => {
      reqLogin(data).then(res => {
        console.log(res.data)
        // console.log('no')
        context.commit('setToken', res.data)
        setToken(res.data)
        resolve(res)
      }).catch(err => {
        console.log(err)
        reject(err)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
